#include "configwin.hpp"

using namespace Gtk;
using namespace Glib;
using namespace sigc;
using namespace std;
using namespace Gnome;

void ConfigWin::Init()
{
     refXml=Glade::Xml::create("storwords.glade");
     refXml->get_widget("configwin",configwin);
     refXml->get_widget("entServer",entServer);
     refXml->get_widget("entFile",entFile);
     refXml->get_widget("entUser",entUser);
     refXml->get_widget("entPass",entPass);
     refXml->get_widget("entKey",entKey);
     refXml->get_widget("btnKeyGen",btnKeyGen);
     refXml->get_widget("btnImport",btnImport);
     refXml->get_widget("btnExport",btnExport);
     refXml->get_widget("btnCancel",btnCancel);
     refXml->get_widget("btnOK",btnOK);
     
     btnKeyGen->signal_clicked().connect(mem_fun(this, &ConfigWin::btnKeyGen_Click));
     btnImport->signal_clicked().connect(mem_fun(this, &ConfigWin::btnImport_Click));
     btnExport->signal_clicked().connect(mem_fun(this, &ConfigWin::btnExport_Click));
     btnCancel->signal_clicked().connect(mem_fun(this, &ConfigWin::btnCancel_Click));
     btnOK->signal_clicked().connect(mem_fun(this, &ConfigWin::btnOK_Click));
}

ConfigWin::ConfigWin(bool firstrun){
	 FirstRun=firstrun;
     Init();
}

void ConfigWin::Run(const Window *parent){
     if(parent!=NULL){
            configwin->set_transient_for(*const_cast<Window*>(parent));
            configwin->set_position(WIN_POS_CENTER_ON_PARENT);
     }
     configwin->property_visible().set_value(true);
     if(!FirstRun){
        entServer->set_text(SettingsManager.GetSettings().Url);
        entFile->set_text(SettingsManager.GetSettings().Path);
        entUser->set_text(SettingsManager.GetSettings().User);
        entPass->set_text(SettingsManager.GetSettings().Pass);
        entKey->set_text(SettingsManager.GetSettings().EncKey);
     }     
     Main::run(*configwin);
}

void ConfigWin::btnKeyGen_Click() {}

void ConfigWin::btnImport_Click() {}

void ConfigWin::btnExport_Click() {}

void ConfigWin::btnCancel_Click() {
	if(FirstRun){
		MessageDialog msg(*configwin,"Are you sure you wish to quit StorWords?",false,MESSAGE_WARNING,BUTTONS_YES_NO,true);
		int res=msg.run();
		if(res==RESPONSE_YES){
			configwin->hide();
			Main::quit();
		}
	}else{
        configwin->hide();
    }
}

void ConfigWin::btnOK_Click(){
    AppSettings newsettings;
    newsettings.Url=entServer->get_text();
    newsettings.Path=entFile->get_text();
    newsettings.User=entUser->get_text();
    newsettings.Pass=entPass->get_text();
    newsettings.EncKey=entKey->get_text();
    
    SettingsManager.SetSettings(newsettings);
    SettingsManager.SaveSettings();
    
    configwin->hide();
}
